<?php

namespace App\Http\Controllers;

use App\Models\AddressBook;
use App\Models\AddressBookLabel;
use App\Models\AddressBookRelateLabel;
use App\Models\AutoFollowPlan;
use App\Models\FollowPlanTemplateLib;
use App\Models\GroupAssistant;
use App\Models\GroupAssistantLog;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;
class AddressBookController extends BaseController
{
    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加通讯录
     */
    public function addBook(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        if (empty($param['user_name'])) {
            return $this->ResultJson(0, "名称不能为空!", []);
        }
        if (empty($param['sex'])) {
            return $this->ResultJson(0, "性别不能为空!", []);
        }
        if (empty($param['age'])) {
            return $this->ResultJson(0, "年龄不能为空!", []);
        }
        if (empty($param['phone'])) {
            return $this->ResultJson(0, "电话不能为空!", []);
        }
        if (empty($param['image'])) {
            return $this->ResultJson(0, "图片不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $result  = (new AddressBook())->add($param);
        if ($result===true) {
            return $this->ResultJson(1, "签到或报道成功!", []);
        }
        return $this->ResultJson(0, "报道失败!", []);
   }

    public function addRemarks(Request $request)
    {
        $param = $request->input();
        if (empty($param['book_id'])) {
            return $this->ResultJson(0, "通讯录ID不能为空!", []);
        }
        if (empty($param['remarks'])) {
            return $this->ResultJson(0, "备注不能为空!", []);
        }
        $res = (new AddressBook())->edit($param);
        if ($res) {
            return $this->ResultJson(1, "修改成功!", []);
        }
    }
   


    /***
     * @param Request $request
     * 通讯录列表
     */
    public function getAddressBookList(Request $request)
    {
        $param = $request->input();
//        if (empty($param['type'])) {
//            return $this->ResultJson(0, "类型不能为空!", []);
//        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        if(isset($param['start_time']) && isset($param['end_time']) && (strtotime($param['start_time']) > strtotime($param['end_time'])) ){
            return $this->ResultJson(0, "开始时间不能大于结束时间!", []);
        }
        $result  = (new AddressBook())->getAddressBookList($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!",$result);
        }
        return $this->ResultJson(0, "获取失败!", []);
    }

    /***
     * @param Request $request
     * 通讯录列表Android
     */
    public function getAddressBookListAndroid(Request $request)
    {
        $param = $request->input();
//        if (empty($param['type'])) {
//            return $this->ResultJson(0, "类型不能为空!", []);
//        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        if(isset($param['start_time']) && isset($param['end_time']) && (strtotime($param['start_time']) > strtotime($param['end_time'])) ){
            return $this->ResultJson(0, "开始时间不能大于结束时间!", []);
        }
        $result  = (new AddressBook())->getAddressBookListAndroid($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!",$result);
        }
        return $this->ResultJson(0, "报道失败!", []);
    }

   /***
     * @param Request $request
     * 通讯录患者详情
     */
    public function getAddressBookDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['book_id'])) {
            return $this->ResultJson(0, "通讯录ID不能为空!", []);
        }
        $result  = (new AddressBook())->getAddressBookDetail($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!",$result);
        }
        return $this->ResultJson(0, "报道失败!", []);
    }


    /***
     * @param Request $request
     * 添加通讯录标签
     */
    public function addAddressBookLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['label_name'])) {
            return $this->ResultJson(0, "标签名称不能为空!", []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        if (empty($param['lab_type'])) {
            return $this->ResultJson(0, "标签类型不能为空!", []);
        }
        $result  = (new AddressBookLabel())->addAddressBookLabel($param);
        if ($result === true) {
            if(isset($param['id'])){
                return $this->ResultJson(1, "编辑成功!",[]);
            }else{
                return $this->ResultJson(1, "添加成功!",[]);
            }
        }
        return $this->ResultJson(0, $result, []);
    }


    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取用户所有标签
     */
    public function getUserAddressBookLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['address_book_id'])) {
            return $this->ResultJson(0, "病例id不能为空!", []);
        }
        $result = (new AddressBookRelateLabel())->getUserAddressBookLabel($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(0, $result, []);
    }




    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 删除用户标签
     */
    public function delUserAddressBookLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['label_id'])) {
            return $this->ResultJson(0, "标签ID不能为空!", []);
        }
        if (empty($param['address_book_id'])) {
            return $this->ResultJson(0, "病例id不能为空!", []);
        }
        $result = (new AddressBookLabel())->delUserAddressBookLabel($param);
        if ($result === true) {
            return $this->ResultJson(1, "删除成功!", []);
        }
        return $this->ResultJson(0, $result, []);
    }

    /***
     * @param Request $request
     * 删除通讯录标签
     */
    public function delAddressBookLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, "标签ID不能为空!", []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        $result = (new AddressBookLabel())->delAddressBookLabel($param);
        if ($result === true) {
            return $this->ResultJson(1, "删除成功!", []);
        }
        return $this->ResultJson(0, $result, []);
    }

    /***
     * @param Request $request
     * 获取通讯录标签列表
     */
    public function getAddressBookLabelList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        $result = (new AddressBookLabel())->getList($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(1, "暂无数据", []);
    }

    /**
     * 批量打标签
     *
     */
    public function giveLabelToUser(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        if (empty($param['book_id'])) {
            return $this->ResultJson(0, "通讯录ID不能为空!", []);
        }
        if (empty($param['label_id'])) {
            return $this->ResultJson(0, "标签ID不能为空!", []);
        }
        $result = (new AddressBookLabel())->giveLabelToUser($param);
        if ($result ===true) {
            return $this->ResultJson(1, "成功!", []);
        }
        return $this->ResultJson(0, $result, []);


    }

    /***
     *    随访--群发
     */
    public function addGroupAssistants(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室不能为空!", []);
        }
        if (empty($param['book_ids'])) {
            return $this->ResultJson(0, "通讯录ID不能为空!", []);
        }
        if (empty($param['content']) && empty($param['sound']) && empty($param['video']) && empty($param['images'])) {
            return $this->ResultJson(0, "群随访内容不能为空!", []);
        }
        $result = (new GroupAssistant())->add($param);
        if (is_array($result)) {
            return $this->ResultJson(1, "发送成功!", $result);
        }
        return $this->ResultJson(0, $result, []);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 历史群发详情
     */
    public function getGroupAssistantsDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['group_id'])) {
            return $this->ResultJson(0, "群发ID不能为空!", []);
        }
        $result = (new GroupAssistant())->detail($param);
        return $this->ResultJson(1, "获取成功!", $result);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 群发消息记录
     */
    public function getGroupAssistantsLog(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $result = (new GroupAssistantLog())->getList($param);
        return $this->ResultJson(1, "获取成功!", $result);
    }

    /***
     *
     * 接诊记录
     */
    public function receivePatientLog(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户id不能为空!", []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        $res = (new Order)->getReceivePatientLog($param);

        return $this->ResultJson(1, "获取成功!", $res);
    }

    /***
     *
     * 新建自动随访
     */
    public function addAutoPlan(Request $request)
    {
        $param = $request->input();
        if (empty($param['auto_id'])) {
            if (empty($param['doctor_id'])) {
                return $this->ResultJson(0, "医师id不能为空!", []);
            }
            if (empty($param['work_id'])) {
                return $this->ResultJson(0, "工作室id不能为空!", []);
            }
            if (empty($param['book_ids'])) {
                return $this->ResultJson(0, "通讯录id不能为空!", []);
            }
            if (empty($param['title'])) {
                return $this->ResultJson(0, "标题不能为空!", []);
            }
            if (empty($param['content'])) {
                return $this->ResultJson(0, "内容不能为空!", []);
            }
        }
        if (empty($param['message'])) {
            return $this->ResultJson(0, "消息内容不能为空!", []);
        }
        if (empty($param['date'])) {
            return $this->ResultJson(0, "时间不能为空!", []);
        }
        if (strtotime($param['date']) < time()) {
            return $this->ResultJson(0, "该时间已过,不能添加计划任务!", []);
        }
        $res = (new AutoFollowPlan())->add($param);
        if(is_array($res)){
            return $this->ResultJson(1, "添加成功!", $res);
        }
        return $this->ResultJson(0, $res, []);
    }


    /***
     *
     * 引入模板添加随访计划
     */
    public function addAutoPlanByTemplate(Request $request)
    {

        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        if (empty($param['book_ids'])) {
            return $this->ResultJson(0, "通讯录id不能为空!", []);
        }
        if (empty($param['template_id'])) {
            return $this->ResultJson(0, "模板id不能为空!", []);
        }
        if (empty($param['infos'])) {
            return $this->ResultJson(0, "随访消息内容和时间不能为空!", []);
        }
        foreach ($param['infos'] as $k => $v) {
            if (strtotime($v['date']) < time()) {
                return $this->ResultJson(0, "该时间已过,不能添加计划任务!", []);
            }
        }
        $res = (new AutoFollowPlan())->addByTemplate($param);
        if(is_array($res)){
            return $this->ResultJson(1, "添加成功!", $res);
        }
        return $this->ResultJson(0, $res, []);
    }


    /**
     * 自动随访引入模板Android
     */

    public function addAutoPlanByTemplateAndroid(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        if(!isset($param['id'])){
            if (empty($param['book_ids'])) {
                return $this->ResultJson(0, "通讯录id不能为空!", []);
            }
            if (empty($param['template_id'])) {
                return $this->ResultJson(0, "模板id不能为空!", []);
            }
        }
        if (!isset($param['key'])) {
            return $this->ResultJson(0, "消息顺序不能为空!", []);
        }
//        if (empty($param['message'])) {
//            return $this->ResultJson(0, "消息内容不能为空!", []);
//        }
//        if (strtotime($param['date']) < time()) {
//            return $this->ResultJson(0, "改时间已过,不能添加计划任务!", []);
//        }
        $res = (new AutoFollowPlan())->addByTemplateForAndroid($param);
        if(is_array($res)){
            return $this->ResultJson(1, "成功!", $res);
        }
        return $this->ResultJson(0, $res, []);
        
    }





    /***
     *
     * 获取自动随访
     */
    public function getAutoPlan(Request $request)
    {
        $param = $request->input();
//        if (empty($param['doctor_id'])) {
//            return $this->ResultJson(0, "医师id不能为空!", []);
//        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        $res = (new AutoFollowPlan())->getList($param);
        if($res){
            return $this->ResultJson(1, "获取成功!", $res);
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * 自动随访详情
     */

    public function getAutoPlanDetail(Request $request)
    {
        $param = $request->input();
//        if (empty($param['doctor_id'])) {
//            return $this->ResultJson(0, "医师id不能为空!", []);
//        }
        if (empty($param['id'])) {
            return $this->ResultJson(0, "随访id不能为空!", []);
        }
        $res = (new AutoFollowPlan())->getDetail($param);
        if($res){
            return $this->ResultJson(1, "获取成功!", $res);
        }
    }

 /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * 自动随访模板详情
     */

    public function getTemplateDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, "随访模板id不能为空!", []);
        }
        $res = (new FollowPlanTemplateLib())->detail($param['id']);
        if($res){
            return $this->ResultJson(1, "获取成功!", $res);
        }
    }



    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * 自动随访模板列表
     */

    public function getTemplateList(Request $request)
    {
        $param = $request->input();
//        if (empty($param['id'])) {
//            return $this->ResultJson(0, "随访模板id不能为空!", []);
//        }
        $res = (new FollowPlanTemplateLib())->getList($param);
        if($res){
            return $this->ResultJson(1, "获取成功!", $res);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 发送问卷调查
     */
    public function addQuestionnaireSurvey(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_ids'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['url'])) {
            return $this->ResultJson(0, "问卷url不能为空!", []);
        }
        //判断url是否正确
        $url = substr($param['url'],8);
        $num = explode('/',$url);
        if(count($num) != 5){
            return $this->ResultJson(0, "问卷链接不正确!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        $res = (new AddressBook())->addQuestionnaireSurvey($param);
        if ($res) {
            return $this->ResultJson(1, "发送成功!", []);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }
    




}
